$(document).ready(function(){
    var title_ms=$('#title_ms').val();
    var liked=$('#liked').val();
    var share_success=$('#share_success').val();
    var email_er = $('#email_er').val();
    var mes_er = $('#mes_er').val();
    var send_success = $('#send_success').val();
    var send_error = $('#send_error').val();
    
    $("#check_like").click(function(){
        var id = $(this).attr('rel');
        
        $.ajax({
            url: root + "contest/like",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                if(data!='false'){
                    $("#count_like").html(data);                   
                }else{
                    $.fancybox('<div style="padding:0px;width:200px;text-align:center;background:#fff"><div class="title">'+title_ms+'</div><p style="padding:10px">'+liked+'</p></div>',{
                        'autoScale':false,
                        'padding':0
                    });
                }
            }
        });
        return false;
    });
    
    $("#like_mo").click(function(){
        var id = $(this).attr('rel');
        var $this=$(this);
        $.ajax({
            url: root + "contest/like_mo",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                if(data!='false'){
                    
                    $this.html(data);
                    $this.attr('id','');
                }else{
                    $.fancybox('<div style="padding:0px;width:200px;text-align:center;background:#fff"><div class="title">'+title_ms+'</div><p style="padding:10px">'+liked+'</p></div>',{
                        'autoScale':false,
                        'padding':0
                    });
                }
            }
        });
        return false;
    });
      
    $(".check_like2").click(function(){
        var id = $(this).attr('rel');
        var $this=$(this)
        $.ajax({
            url: root + "contest/like",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                if(data!='false'){
                    $this.parent().find('.check_like2').html(data);
                }else{
                    $.fancybox('<div style="padding:0px;width:200px;text-align:center;background:#fff"><div class="title">'+title_ms+'</div><p style="padding:10px">'+liked+'</p></div>',{
                        'autoScale':false,
                        'padding':0
                    });
                }
            }
        });
        return false;
    });
    
    $(".check_like").click(function(){
        var id = $(this).attr('rel');
        var $this=$(this)
        $.ajax({
            url: root + "contest/like_mo",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                if(data!='false'){
                    $this.parent().find('.check_like').html(data);
                }else{
                    $.fancybox('<div style="padding:0px;width:200px;text-align:center;background:#fff"><div class="title">'+title_ms+'</div><p style="padding:10px">'+liked+'</p></div>',{
                        'autoScale':false,
                        'padding':0
                    });
                }
            }
        });
        return false;
    });
	
    $(".comment_like").click(function(){
        var id = $(this).attr('id');
        $.ajax({
            url: root + "contest/comment_like",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                if(data!='false'){
                    $("#like_"+ id).html(data);
                }else{
                    $.fancybox('<div style="padding:0px;width:200px;text-align:center"><div class="title">'+title_ms+'</div><p style="padding:10px">'+liked+'</p></div>',{
                        'autoScale':false,
                        'padding':0
                    });
                }
            }
        });
        return false;
    });


    $("#share_fb, #share_twitter").live('click',function(){
        var $this=$(this);
        var id= $('#ids').val();
        var url = $('#urls').val();
        var img =  $('#imgs').val();
        var title =  $('#titles').val();
        var des =  $('#des_invite').val();
        $.ajax({
            url: root + "contest/share",
            type: "POST",
            data: {
                id : id
            },
            success: function(data){
                
                if(data!='false'){
                    $("#count_share").html(data);
                }
                if($this.is('#share_fb')){
                    share_facebook(url,title,des,img);
                }else{
                    share_twitter(url,title);
                }
                
                $.fancybox('<div style="padding:0px;width:200px;text-align:center;background:#fff"><div class="title">'+title_ms+'</div><p style="padding:10px">'+share_success+'</p></div>',{
                    'autoScale':false,
                    'padding':0
                });
            }
        });
        return false;
    });

    // set radius button

    $('.searchicon').click(function(){
        $('#search').submit();
    });
    $('#typesort').bind('change', function(){
        var url=$(this).val();
        window.location=url;
    })
    
    function share_facebook(u,t,s,img) {
        var url="http://www.facebook.com/sharer.php?s=100&p[title]="+encodeURIComponent(t)+"&p[url]="+encodeURIComponent(u)+"&p[summary]="+encodeURIComponent(s)+"&p[images][0]="+encodeURIComponent(img);
        window.open(url);
        return false;
    }
    function share_twitter(url,title) {
        window.open("http://twitter.com/home?status="+ title + encodeURIComponent(url));
    }
    
});


function invite()
{
    var mes_invite=$('#mes_invite').val();
    FB.ui({
        method: 'apprequests',
        message: mes_invite
    }, function (response) {
        if (response && response.to) {
                
        }
    });
    return false;     
}

function invite2(link,img,title)
{
    var title_invite_contest=$('#title_invite_contest').val();
    var des_invite=$('#des_invite').val();
    FB.ui({
        method: 'apprequests',
        message: title_invite_contest
    }, function (response) {
        if (response && response.to) {
            var id=String(response.to);
            //var _link = root+_link1;
            
            $.each(id.split(','), function(key, value) { 
                FB.api('/'+value+'/feed', 'post', {
                    //message: link,
                    picture: img,
                    caption: decodeURIComponent(title),
                    name: decodeURIComponent(title),
                    link:link,
                    description:des_invite
                }, function(response) {
                    if (!response || response.error) {
                        console.log('Error occured');
                    } else {
                        console.log('Post ID: ' + response.id);
                    }
                }); 
            });
        }
    });
    return false;     
}



$(document).ready(function(){
    $('a.back').click(function(){
        window.history.back();
        return false;
    });
});
